const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowSkinUrl: "",
    heightScreen: 1200,
    grade: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var globalSkinUrl = app.globalSkinUrl;
    that.getLevel();
    var skinUrl_grade = [];
    for (var i = 0; i < globalSkinUrl.length; i++) {
      skinUrl_grade.push(globalSkinUrl[i]);
    }
    that.setData({
      skinUrl_grade: skinUrl_grade,
    });

    //获取屏幕高宽
    wx.getSystemInfo({
      success: function(res) {
        //rpx
        that.setData({
          heightScreen: res.windowHeight * (750 / res.windowWidth),
        });
      }
    });

  },
  getLevel: function() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    wx.request({
      url: app.globalData.targetUrl + "users/" + user_id + '/',
      success: function(res) {
        var user = res.data.data;
        that.setData({
          grade: user.level
        })
      }
    })
  },
  onClickSkin: function(e) {
    var that = this;
    var globalSkinUrl = app.globalSkinUrl;
    var index = e.currentTarget.id;
    if (that.data.grade < globalSkinUrl[index].level) {
      wx.showModal({
        title: '',
        content: '等级不足, 快去发帖赚取积分叭！',
        showCancel: false
      })
    } else {
      app.globalData.nowSkinUrl = globalSkinUrl[index].image_url;
      if (globalSkinUrl[index].is_dark == 0) {
        app.globalData.nowColor = "#0086ff";
      } else {
        app.globalData.nowColor = "#fff";
      }
      that.setData({
        nowSkinUrl: app.globalData.nowSkinUrl
      });
      that.setNowSkin(parseInt(index) + 1);
    }


  },
  //发送请求更改皮肤
  setNowSkin: function(id) {
    console.log("id:", id);
    var that = this;
    var user_id = wx.getStorageSync("user_id");
    wx.request({
      url: app.globalData.targetUrl + "users/" + user_id + "/",
      method: "put",
      data: {
        wallpaper_id: id
      },
      success: res => {
        console.log("wallpaper", res.data);
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            wx.switchTab({
              url: "/pages/my/my"
            });
          }
        }
      })
    }
    that.setData({
      nowSkinUrl: app.globalData.nowSkinUrl
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})